/*
 * Copyright (c) 2011, Eriq Muhammad Adams J.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Institute Technology of Sepuluh Nopember nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ERIQ MUHAMMAD ADAMS J. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.pathmachine.ui.renderer.impl;

import com.googlecode.pathmachine.mm.Map;
import com.googlecode.pathmachine.mm.Point3D;
import com.googlecode.pathmachine.mm.TTerrain;
import com.googlecode.pathmachine.ui.renderer.MapRenderer;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
import java.util.ArrayList;
import java.util.List;
import jme3tools.optimize.GeometryBatchFactory;

/**
 *
 * @author <a href="mailto:eriq.adams@gmail.com">Eriq Muhammad Adams J.</a> 
 */
public class CraftRenderer implements MapRenderer {

    public Node render(Map map, AssetManager assetManager) {
        float halfTileSize = map.getTileSize() / 2;
        float height = 0.00f;
        Box ground = new Box(Vector3f.ZERO, halfTileSize, height, halfTileSize);
        Material groundMat = assetManager.loadMaterial("Textures/craft/ground.j3m");
        Geometry groundGeom = new Geometry("g", ground);
        float height2 = map.getTileSize();
        Box box = new Box(Vector3f.ZERO, halfTileSize, height2, halfTileSize);
        Material boxMat = assetManager.loadMaterial("Textures/craft/obstacle.j3m");
        Geometry boxGeom = new Geometry("b", box);
        List<Geometry> groundGeometries = new ArrayList<Geometry>();
        List<Geometry> boxGeometries = new ArrayList<Geometry>();
        Node node = new Node("map");
        Vector3f loc = new Vector3f();
        for (int x = 0; x < map.getMapWidth(); x++) {
            for (int y = 0; y < map.getMapHeight(); y++) {
                Point3D _temp = map.getOpenGLCoord(x, y);
                loc.x = _temp.x;
                loc.y = _temp.y;
                loc.z = _temp.z;
                TTerrain type = map.getTerrainType(x, y);
                if (TTerrain.kGround == type) {
                    Geometry geom = groundGeom.clone();
                    loc.setY(loc.getY() + height);
                    geom.move(loc);
                    loc.setY(loc.getY() - height);
                    groundGeometries.add(geom);
                } else if (TTerrain.kOutOfBounds == type) {
                    if (((x + 1) < map.getMapWidth() && TTerrain.kGround == map.getTerrainType(x + 1, y))
                            || (x - 1) >= 0 && TTerrain.kGround == map.getTerrainType(x - 1, y)
                            || (y + 1) < map.getMapHeight() && TTerrain.kGround == map.getTerrainType(x, y + 1)
                            || (y - 1) >= 0 && TTerrain.kGround == map.getTerrainType(x, y - 1)) {
                        Geometry geom = boxGeom.clone();
                        loc.setY(loc.getY() + height2);
                        geom.move(loc);
                        loc.setY(loc.getY() - height2);
                        boxGeometries.add(geom);
                    }
                }
            }
        }

        // Merge geometries
        Mesh groundMesh = new Mesh();
        GeometryBatchFactory.mergeGeometries(groundGeometries, groundMesh);
        Geometry groundBatchGeom = new Geometry("Ground", groundMesh);
        groundBatchGeom.setMaterial(groundMat);
        node.attachChild(groundBatchGeom);
        groundGeometries.clear();

        Mesh boxMesh = new Mesh();
        GeometryBatchFactory.mergeGeometries(boxGeometries, boxMesh);
        Geometry boxBatchGeom = new Geometry("Box", boxMesh);
        boxBatchGeom.setMaterial(boxMat);
        node.attachChild(boxBatchGeom);
        boxGeometries.clear();

        Box landscape = new Box(map.getMapWidth() * halfTileSize, 0f, map.getMapHeight() * halfTileSize);
        landscape.scaleTextureCoordinates(new Vector2f(40, 40));
        Geometry landscapeGeom = new Geometry("geom", landscape);
        Material landscapeMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        landscapeMat.setColor("Color", new ColorRGBA(31 / 255f, 25 / 255f, 27 / 255f, 1f));
        landscapeGeom.setMaterial(landscapeMat);
        landscapeGeom.move(0f, -0.01f, 0f);
        node.attachChild(landscapeGeom);
        
        // dereferencing object
        groundMat = null;
        groundGeom = null;
        landscapeGeom = null;
        landscapeMat = null;


        return (Node) GeometryBatchFactory.optimize(node);
    }
}
